package com.example.xf.Controller;

import com.alibaba.fastjson.JSON;
import com.example.xf.FeClient.FtpUtil;
import com.example.xf.Service.purchaseService;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.github.pagehelper.PageInfo;
import dto.Dto;

import feign.RequestLine;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pojo.*;
import until.OrderCoderUtil;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@RestController
public class PurchaseController {
    @Autowired
    private purchaseService purchaseService;


    @RequestMapping("addPo")
    public Dto<Integer> addPo(@RequestParam(value = "file", required = false) MultipartFile file,
                              @RequestParam(value = "json", required = false) String json) {
        Purchase  pco= JSON.parseObject(json,Purchase.class);

        if (file!=null){
            FtpUtil ftpUtil=new FtpUtil();
            String oldername=file.getOriginalFilename();
            String suffix= FilenameUtils.getExtension(oldername);
            String newname= UUID.randomUUID().toString().replace("-", "").substring(15).toLowerCase()+ "."+suffix;
            System.out.println(newname);
            FtpUtil.FtpConfig ftpConfig=new FtpUtil.FtpConfig();
            ftpConfig.setIp("192.168.2.80");
            ftpConfig.setPort(21);
            ftpConfig.setUserName("ftpuser");
            ftpConfig.setPassword("123456");
            pco.getPurchaseOrder().setLog(newname);
            try {
                ftpUtil.upload(ftpConfig,
                        "/tpo/",
                        file.getInputStream(),
                        newname);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        String ddhao=OrderCoderUtil.getCaigouCode(null);
        List<PurchaseList> list=pco.getList();
        for (PurchaseList pu :list){
            pu.setPid(ddhao);
        }
        pco.getPurchaseOrder().setPid(ddhao);
        Dto dto=purchaseService.addPo(pco);
        return dto;
    }

    @RequestMapping("delePo")
    public Dto<Integer> delePo(@RequestParam("id") String id) {
        return purchaseService.delePo(id);
    }

    @RequestMapping("updaPo")
    public Dto<Integer> updaPo(@RequestBody PurchaseOrder purchaseOrder) {
        return purchaseService.updaPo(purchaseOrder);
    }
    @RequestMapping("ListPo")
    public Dto<PageInfo<PurchaseOrder>> ListPo(@RequestBody ListPo listPo) {
        return purchaseService.ListPo(listPo);
    }

    @RequestMapping("contPo")
    public Dto<Integer> contPo(){
        return purchaseService.contPo();
    }
    @RequestMapping("showPid")
    public Dto<List<String>> showPid(){
        return purchaseService.showPid();
    }

    @RequestMapping("show1Po")
    public Dto<PurchaseOrder> show1Po(@RequestParam("pid") String pid){
        return purchaseService.show1Po(pid);
    }
    @RequestMapping("updaPo2")
    public Dto<Integer> updaPo2(@RequestBody Xgzt xgzt){
        return purchaseService.updaPo2(xgzt);
    }

    /**
     * @param list
     * @return
     */
    @RequestMapping("addPl")
    public Dto<Integer> addPl(@RequestBody List<PurchaseList> list) {

        return purchaseService.addPl(list);
    }

    @RequestMapping("listPl")
    public Dto<List> listPl(@RequestParam("pid") String pid) {

        return purchaseService.listPl(pid);
    }
    @RequestMapping("delePl")
    public Dto<Integer> delePl(@RequestParam("id") String id){
        return  purchaseService.delePl(id);
    }

    @RequestMapping("updaPl")
    public Dto<Integer> updaPl(@RequestBody Xgth xgth){
        return purchaseService.updaPl(xgth);
    }

    /**
     * @param file
     * @param json
     * @return
     */
    @RequestMapping("addPr")
    public Dto<Integer> addPr(@RequestParam(value = "file", required = false) MultipartFile file,
                              @RequestParam(value = "json", required = false) String json) {
        Pr pr =JSON.parseObject(json,Pr.class);
        if(file!=null){
            FtpUtil ftpUtil=new FtpUtil();
            String oldername=file.getOriginalFilename();
            String suffix= FilenameUtils.getExtension(oldername);
            String newname= UUID.randomUUID().toString().replace("-", "").substring(15).toLowerCase()+ "."+suffix;
            System.out.println(newname);
            FtpUtil.FtpConfig ftpConfig=new FtpUtil.FtpConfig();
            ftpConfig.setIp("192.168.2.80");
            ftpConfig.setPort(21);
            ftpConfig.setUserName("ftpuser");
            ftpConfig.setPassword("123456");
            pr.getPurchaseReturn().setLog(newname);
            try {
                ftpUtil.upload(ftpConfig,
                        "/tpo/",
                        file.getInputStream(),
                        newname);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        String ddhao=OrderCoderUtil.getCaigoutuihuoCode(null);
        List<PurchaseList> list=pr.getList();
        for (PurchaseList pur :list){
            pur.setPid(ddhao);

        }
        pr.getPurchaseReturn().setCode(ddhao);
        Dto dto=purchaseService.addPr(pr);
        return dto;
    }

    @RequestMapping("delePr")
    public Dto<Integer> delePr(String id) {
        return purchaseService.delePr(id);
    }



    @RequestMapping("listPr")
    public Dto<PageInfo<PurchaseReturn>> listPr(@RequestBody ListPo listPo) {

        return purchaseService.listPr(listPo);
    }
    @RequestMapping("contPr")
    public Dto<Integer> contPr(){
        return purchaseService.contPr();
    }

    @RequestMapping("showPr")
    public Dto<PurchaseReturn> showPr(@RequestParam("code") String code){
        return purchaseService.showPr(code);
    }

    @RequestMapping("updaPr")
    public Dto<Integer> updaPr(@RequestBody Xgzt xgzt){
        return purchaseService.updaPr(xgzt);
    }
    /**
     * @param supplier
     * 供应商模块
     * @return
     */
    @RequestMapping("addSp")
    public Dto<Integer> addSp(@RequestBody Supplier supplier) {
        return purchaseService.addSp(supplier);
    }

    @RequestMapping("deleSp")
    public Dto<Integer> deleSp(@RequestParam("id") int id) {
        return purchaseService.deleSp(id);
    }

    @RequestMapping("updaSp")
    public Dto<Integer> updaSp(@RequestBody Supplier supplier) {
        return purchaseService.updaSp(supplier);
    }

    @RequestMapping("listSp")
    public Dto<PageInfo<Supplier>> listSp(@RequestParam("name") String name,
                                          @RequestParam("pageNum") int pageNum,
                                          @RequestParam("pageSize") int pageSize) {
        return purchaseService.listSp(name,pageNum,pageSize);
    }

    @RequestMapping("contSp")
    public Dto<Integer> contSp() {
        return purchaseService.contSp();
    }


    @RequestMapping("nameSp")
    public Dto<List<String>> nameSp(){
        return purchaseService.nameSp();
    }

    @RequestMapping("showSp")
    public Dto<Supplier> showSp(@RequestParam("id") Integer id){
        return purchaseService.showSp(id);
    }

    @RequestMapping("showSp2")
    public Dto<Supplier> showSp2(@RequestParam("name") String name){
        return purchaseService.showSp2(name);
    }

    /**
     * @param house_order
     * 仓库模块
     * @return
     */
    @RequestMapping("addHo")
    public Dto<Integer> addHo(@RequestBody House_order house_order){
        return purchaseService.addHo(house_order);
    }
    @RequestMapping("deleHo")
    public Dto<Integer> deleHo(@RequestParam("id") String id){
        return purchaseService.deleHo(id);
    }
    @RequestMapping("listHo")
    public Dto<PageInfo<House_order>> listHo(@RequestBody ListHo listHo){
        return purchaseService.listHo(listHo);
    }
    @RequestMapping("contHo")
    public Dto<Integer> contHo(){
        return purchaseService.contHo();
    }

}
